/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MathCore.Renders;

import Core.GUICanvas;
import MathCore.Lexeme;
import MathCore.Render;
import MathCore.Symbol;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author Александр
 */
public class Brace extends Atom
{
	private int type = 0;
	public Brace sister = null;
	public static int simpleWidth = 4, listWidth = 3, roundRadius=29;

	public Brace(Symbol lex)
	{
		super(lex);
		type = lex.getType();
	}

	public int width()
	{
		switch(type)
		{
			case Symbol.LEFT_BRACE:
			case Symbol.RIGHT_BRACE:
				return simpleWidth;

			case Symbol.LEFT_LIST:
			case Symbol.RIGHT_LIST:
				return listWidth;
		}
		return 0;
	}

	public int height()
	{
		if(sister != null) return height;
		else
		{
			switch(type)
			{
				case Symbol.LEFT_BRACE:
				case Symbol.LEFT_LIST:
					if(next == null) return Render.getFont().getHeight();
					else return next.height();


				case Symbol.RIGHT_LIST:
				case Symbol.RIGHT_BRACE:
					if(prev == null) return Render.getFont().getHeight();
					else return prev.height();
			}
			return 0;
		}
	}

	public int center()
	{
		if(sister != null) return center;
		else
		{
			switch(type)
			{
				case Symbol.LEFT_BRACE:
				case Symbol.LEFT_LIST:
					if(next == null) return Render.getFont().getHeight()/2;
					else return next.center();


				case Symbol.RIGHT_LIST:
				case Symbol.RIGHT_BRACE:
					if(prev == null) return Render.getFont().getHeight()/2;
					else return prev.center();
			}
			return 0;
		}
	}

	public void draw(Graphics g, int x, int y, int style)
	{
		saveCoordinates(x, y, style);

		// смещаем координаты, учитывая стили (сводим к LEFT|TOP)
		if(style(style, Block.BOTTOM))	y -= height();
		if(style(style, Block.RIGHT))	x -= width();
		if(style(style, Block.HCENTER))	x -= width()/2;
		if(style(style, Block.VCENTER)) y -= height() - center();

		if(g != null)
		{
			
			if(sister != null && (sister == Render.activeBlock || this == Render.activeBlock) && Render.useCursor)
			{
				g.setColor(Render.oneBraceColor);
				g.fillRect(x-1, y+1, width()+3, height()-1);
			}
			else if(sister == null)
			{
				g.setColor(Render.twoBracesColor);
				g.fillRect(x-1, y+1, width()+3, height()-1);
			}
			g.setColor(Render.textColor);
			switch(type)
			{
				case Symbol.LEFT_LIST:
					GUICanvas.clipMashine.clip(g, x, y, width()+1, height()+1);
					g.drawRect(x, y+1, width()*3, height()-2);
					break;

				case Symbol.LEFT_BRACE:
					GUICanvas.clipMashine.clip(g, x, y, width()+1, height()+1);
					g.drawRoundRect(x, y+1, width()*3, height()-2, roundRadius, roundRadius);
					break;

				case Symbol.RIGHT_LIST:
					GUICanvas.clipMashine.clip(g, x-1, y, width()+1, height()+1);
					g.drawRect(x-width()*2-1, y+1, width()*3, height()-2);
					break;

				case Symbol.RIGHT_BRACE:
					GUICanvas.clipMashine.clip(g, x-1, y, width()+1, height()+1);
					g.drawRoundRect(x-width()*2-1, y+1, width()*3, height()-2, roundRadius, roundRadius);
					break;
			}
			GUICanvas.clipMashine.removeTopClip(g);

			if(Render.activeBlock == this && Render.useCursor)
			{
				g.setColor(Render.linkColor);
				g.drawLine(x+width()+1, y, x+width()+1, y+height()-1);
				g.drawLine(x-1, y+height()-1, x+width()+1, y+height()-1);
			}
		}
	}

	public int getType()
	{
		return type;
	}


}
